#include "CommonUtils.h"

#include <QApplication>
#include <QDebug>
#include <QFile>
#include <QPainter>
#include <QSettings>
#include <QWidget>

CommonUtils::CommonUtils() {
}

QPixmap CommonUtils::getRoundImage(const QPixmap& src, QPixmap& mask, QSize masksize) {
    if (masksize == QSize(0, 0)) {
        masksize = mask.size();
    }
    else {
        mask = mask.scaled(masksize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    }

    // 保存转换后的头像
    QImage   resultImage(masksize, QImage::Format_ARGB32_Premultiplied);
    QPainter painter(&resultImage);
    painter.setCompositionMode(QPainter::CompositionMode_Source);
    painter.fillRect(resultImage.rect(), Qt::transparent);
    painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
    painter.drawPixmap(0, 0, mask);
    painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
    painter.drawPixmap(0, 0, src.scaled(masksize, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    painter.end();

    return QPixmap::fromImage(resultImage);
}

QPixmap CommonUtils::getRoundImage(const QPixmap& src, QSize masksize) {
    QPixmap mask(":/resource/img/mainwindow/head_mask.png");
    return getRoundImage(src, mask, masksize);
}

void CommonUtils::loadStyleSheet(QWidget* widget, const QString& sheetName) {
    QFile file(":/css/" + sheetName + ".css");
    file.open(QFile::ReadOnly);

    if (file.isOpen()) {
        widget->setStyleSheet("");
        QString qsstyleSheet = QLatin1String(file.readAll());
        // qDebug() << widget->objectName() << "==> " << widget->styleSheet() << "\n";
        // qDebug() << sheetName << "==> " << qsstyleSheet << "\n\n";
        widget->setStyleSheet(qsstyleSheet);
        file.close();
    }
}

void CommonUtils::addStyleSheet(QWidget* widget, const QString& sheetName) {
    QFile file(":/css/" + sheetName + ".css");
    file.open(QFile::ReadOnly);
    auto style = widget->styleSheet();
    if (file.isOpen()) {
        QString qsstyleSheet = QLatin1String(file.readAll());
        style += qsstyleSheet;
        // qDebug() << widget->objectName() << "==> " << style << "\n";
        // qDebug() << sheetName << "==> " << style << "\n\n";
        widget->setStyleSheet("");
        widget->setStyleSheet(style);
        file.close();
    }
}

void CommonUtils::setDefaultSkinColor(const QColor& color) {
    const QString&& path = QApplication::applicationDirPath() + "/" + QString("tradeprintinfi.ini");
    QSettings       settins(path, QSettings::IniFormat);

    settins.setValue("DefaultSkin/red", color.red());
    settins.setValue("DefaultSkin/green", color.green());
    settins.setValue("DefaultSkin/blue", color.blue());
}

QColor CommonUtils::getDefaultSkinColor() {
    QColor          color;
    const QString&& path = QApplication::applicationDirPath() + "/" + QString("tradeprintinfi.ini");
    if (!QFile::exists(path)) {
        setDefaultSkinColor(QColor(22, 154, 218));
    }
    QSettings settins(path, QSettings::IniFormat);
    color.setRed(settins.value("DefaultSkin/red").toInt());
    color.setGreen(settins.value("DefaultSkin/green").toInt());
    color.setBlue(settins.value("DefaultSkin/blue").toInt());
    return color;
}
